package com.gohome.gohomechat.ui.activity;

import android.Manifest;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.gohome.gohomechat.utils.ToActivityUtil;
import com.google.zxing.Result;

import me.dm7.barcodescanner.zxing.ZXingScannerView;

/**
 * Created by dllo on 16/3/14.
 */
public class ScanActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
    private ZXingScannerView scanView;
    protected String tag = getClass().getSimpleName();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // API23之后需要手动调用获取权限
//        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
//                != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.CAMERA}, 1);
//        }
        scanView = new ZXingScannerView(this);
        setContentView(scanView);
        ActivityControl.addAty(tag, this);

    }


    @Override
    protected void onResume() {
        super.onResume();
        scanView.setResultHandler(this);
        scanView.startCamera();
    }

    @Override
    public void onPause() {
        super.onPause();
        scanView.stopCamera();
    }

    // 扫描成功后会回调此方法
    @Override
    public void handleResult(Result result) {
        Bundle bundle = new Bundle();
        Log.i("wer","二维码名字 "+result.getText());
        bundle.putString("friend", result.getText());
        ToActivityUtil.startToActivity(this, AddFriendActivity.class, bundle);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityControl.removeAty(tag);
    }
}
